CE.THEME = {

    // a cached reference to the currently active parent theme.
    active_theme: null,
    init: function () {
        // re-structure themes into a nice parent-child relationship.
        // note that we subtract one from theme indices fetched from the database 
        // because they are zero-based in javascript arrays
        // but one-based in the SQL database.

	// it seems redundant to do this work here when the menu.render method can sort all on its own,
	// however, ordering the source data (CE.THEME.themes) on init allows the themes to show up ordered
	// even when you simply loop over them with no sorting (like in the Add Media -> Theme dropdowns)
	var themes_sorted = new Object;
	var sk = CE.UTIL.sorted_keys(CE.THEME.themes, 'order');

	for (key in sk) {
        if (sk.hasOwnProperty(key))
		    themes_sorted[sk[key]] = CE.THEME.themes[sk[key]];
	}

	CE.THEME.themes = themes_sorted;

	var sub_theme_id, themes = CE.THEME.themes, theme_id;

        for (theme_id in themes) {

            for (var sub_theme_num = 0; themes[theme_id] && sub_theme_num < themes[theme_id].sub_themes.length; sub_theme_num++) {
                sub_theme_id = themes[theme_id].sub_themes[sub_theme_num];
                if (themes[sub_theme_id]) {
                    themes[sub_theme_id].parent_theme = themes[theme_id];
                } else {
                    console.warn('bad reference to theme of id `' + sub_theme_id + '`.');
                }
            }
            // default dates
            CE.UTIL.default_dates(themes[theme_id]);
        }
        // a cached reference to the actual menu element.
        this.menu.$ = $("#ce_theme_menu");
        this.menu.render();
    },

    get_by_title: function (title) {
        for (i in CE.THEME.themes) {
            if (CE.THEME.themes[i].title === title) return i;
        }
    },

    menu: {

        click: function (evt) {
            // handle theme menu clicks.
            var m = evt.target.id.match(/theme-(\d+)/);
            if (m && m.length) {
                CE.THEME.set(m[1]);
            }
        },

        // generates a menu item, highlighted if active.
        item: function (theme) {
            var $item = $.touch('div').attr({
                id: 'theme-' + theme.id
            }).text(theme.title);
            if (theme.active) {
                theme.active && $item.addClass('active');
            }
            return $item;
        },

        render: function () {
            var k, sk = CE.UTIL.sorted_keys(CE.THEME.themes, 'order');
            for (var i in sk) {
            	if (sk.hasOwnProperty(i)) {
	                k = sk[i];
	                if (!CE.THEME.themes[k].parent_theme) {
	                    this.$.append(this.item(CE.THEME.themes[k]));
	                }
            	}
            }
        },
		
        // highlights active theme(s).
        activate: function () {
            var sub_theme, theme = CE.THEME.active_theme;
            $("#ce_menu_theme").text(CE.THEME.active_theme.title);
            $('div.active', this.$).removeClass('active');
            $('#theme-' + theme.id).addClass('active');
            if (theme.sub_themes.length === 0) {
                CE.WIDGETS.MENU.close_all();
            } else {
                // clear existing sub themes.
                $('div div', this.$).remove();
				var sub_themes = [];
				for (var i=0; i < theme.sub_themes.length; i++) {
					sub_themes.push(CE.THEME.themes[theme.sub_themes[i]]);
				}
				var sk = CE.UTIL.sorted_keys(sub_themes, 'order');
                for (var i in sk) {
				    if (sk.hasOwnProperty(i)) {
						sub_theme = sub_themes[sk[i]];
						//sub_theme.active = true;
						$('#theme-' + theme.id).append(this.item(sub_theme));
					}
                }
            }
        }
    },

    /*ready: function () {
        CE.WIDGETS.SLIDER.set(CE.START_YEAR);
        this.is_set = true;
    },*/

    is_set: false,
    
    // alter the currently active set of themes.
    set: function (theme_id) {

        var $theme_tab,
        // themes array is offset from database id by 1.
        theme = this.active_theme = this.themes[theme_id];
        for (var i in this.themes) {
            this.themes[i].active = false;
        }
        theme.active = true;  

        $("#ce_tab_theme_content .description").html(CE.UTIL.describe(theme));
        
        // make all sub themes of the active theme also active.
        this.menu.activate();
        CE.START_YEAR = CE.UTIL.date2year(theme.date_start) || 1970;
        CE.END_YEAR = CE.UTIL.date2year(theme.date_end, 2019) || 2019;
        CE.WIDGETS.SLIDER.update();
        CE.LAYERS.refresh();
        CE.map_moved();
        if (CE.controls.select) {
            CE.controls.select.deactivate();
            CE.controls.select.activate();
        }
        CE.WIDGETS.ANIMATION.set_period("last");
        // the first time a theme is set
        /*if (!this.is_set) {
            this.ready();
        }*/

        // display all the informational theme meta data in the theme tab.
        $(window).resize();
        CE.MEDIA.update_all();
    },
 
    // determines if an entity is within the active themes.
    // works for layer and feature.
    contains: function (entity) {
        var in_theme = false, themes = entity.ce.themes, theme;
        for (var theme_id in themes) {
            theme = this.themes[themes[theme_id]];
            in_theme = in_theme || ( theme && theme.active );
                
        }
        return in_theme;
    }
};

